package com.xszx.controller;

import com.xszx.entity.TDept;
import com.xszx.service.TDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@Controller
public class DeptController {

    @Autowired
    private TDeptService deptService;

    /**
     * 遍历所有部门
     * @return
     */
    @RequestMapping("/selectDeptList")
    @ResponseBody
    public List<TDept> selectDeptList(){
        List<TDept> depts = deptService.selectDepts();
        log.info("查询所有部门：{}",depts);
        return depts;
    }
    /**
     * 获取部门人数比例
     * @return
     */
    @RequestMapping("/selectDept")
    @ResponseBody
    public List<TDept> selectDept(){
        List<TDept> depts = deptService.selectDepts();
        return depts;
    }

    /**
     * 添加部门
     * @param
     * @return
     */
    @ResponseBody
    @PostMapping("/addDept")
    public String addDept(@RequestParam("dname") String dname,@RequestParam("dsum") int dsum) {
        TDept dept1 = deptService.getDeptByName(dname);
        TDept dept = new TDept();
        dept.setDsum(dsum);
        dept.setDname(dname);
        log.info("添加部门：{}",dept);
        if (dept1 == null) {
            int i = deptService.addDept(dept);
            System.out.println(i > 0 ? "添加成功！" : "添加失败！");
            System.out.println("受影响的行数为：" + i);
            return "1";
        } else {
            System.out.println("部门已存在");
            return "0";
        }
    }

    /**
     * 根据id删除部门
     * @param
     * @return
     */
    //@RequestParam("id")int id
    @RequestMapping("/deleteDeptById")
    @ResponseBody
    public int deleteDeptById(@RequestParam("did")Long did){
        log.info("根据id删除部门：{}",did);
       int i =  deptService.deleteById(did);
        System.out.println( i> 0 ? "删除成功！" : "删除失败！");
        System.out.println("受影响的行数为：" + i);
       return i;
    }

    //@RequestBody TDept dept
    @PostMapping("/updateDept")
    @ResponseBody
    public int updateDept(TDept dept){
        log.info("修改部门：{}",dept);
        TDept dept1 = deptService.getDeptByName(dept.getDname());
        if (dept1 == null){
            int i = deptService.updateDept(dept);
            System.out.println( i> 0 ? "修改成功！" : "修改失败！");
            System.out.println("受影响的行数为：" + i);
            return i;
        }else {
            if (dept1.getDid().equals(dept.getDid())){
                int i = deptService.updateDept(dept);
                System.out.println( i> 0 ? "修改成功！" : "修改失败！");
                System.out.println("受影响的行数为：" + i);
                return i;
            }else {
                return -1;
            }
        }
    }

    @RequestMapping("/getDeptByDid")
    @ResponseBody
    public TDept getDeptByDid(@RequestParam("did")Long did){
        log.info("根据部门id查找部门，{}",did);
       TDept dept = deptService.getDeptByDid(did);
        System.out.println(dept);
        return dept;
    }

}
